//
//  ForgetPwdViewController.m
//  cherryproduct
//
//  Created by Enter zhao on 14-8-19.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "SettingPwdViewController.h"

@interface ForgetPwdViewController ()

@end

@implementation ForgetPwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"密码找回"];
    self.myView.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.emailField.delegate=self;
    self.signField.delegate=self;
    
    self.emailField.keyboardType=UIKeyboardTypeNumberPad;
    
    self.btn_getNum.titleLabel.textAlignment=NSTextAlignmentCenter;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSTimeInterval animationDuration = 0.30f;
//    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
//    [UIView setAnimationDuration:animationDuration];
//    CGRect rect = CGRectMake(0.0f,0 , self.view.frame.size.width, self.view.frame.size.height);
//    self.view.frame = rect;
//    [UIView commitAnimations];
    [_emailField resignFirstResponder];
    [_signField resignFirstResponder];
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    
//    NSTimeInterval animationDuration = 0.30f;
//    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
//    [UIView setAnimationDuration:animationDuration];
//    CGRect rect = CGRectMake(0.0f,0 , self.view.frame.size.width, self.view.frame.size.height);
//    self.view.frame = rect;
//    [UIView commitAnimations];
    [textField resignFirstResponder];
    
    return YES;
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
    if ([textField isEqual:self.emailField]) {
        self.img_0.image=[UIImage imageNamed:@"longppwordtext"];
    }
    if ([textField isEqual:self.signField]) {
        self.img1.image=[UIImage imageNamed:@"longppwordtext"];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
    if ([textField isEqual:self.emailField]) {
        self.img_0.image=[UIImage imageNamed:@"longppword"];
    }
    if ([textField isEqual:self.signField]) {
        self.img1.image=[UIImage imageNamed:@"longppword"];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)goOutTime
{
    __block int timeout=120; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{

        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.label_num.text=@"获取验证码";
                self.btn_getNum.userInteractionEnabled = YES;
            });
        }else{
            //int minutes = timeout / 60;
            int seconds = timeout;
            NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                NSLog(@"____%@",strTime);
                //[l_timeButton setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                self.label_num.text = strTime;
                self.btn_getNum.userInteractionEnabled = NO;
                
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

- (IBAction)sendEmailAction:(id)sender {
    NSString * str_email = self.emailField.text;
    if (str_email == nil || [str_email isEqual:@""]) {
        [self showAlertView:@"温馨提示" subtitle:@"请输入手机号!"];
        return;
    }
    
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       _emailField.text, @"phone",nil];
    [self.view startLoadingAnimation];
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/send_forgot_phone" paramDictionary:requestDic onCompletion:^(NSString *responseString) {
        [self.view stopLoadingAnimation];
        @try {
            NSDictionary * dic = [responseString JSONValue];
            if ([[dic objectForKey:@"status"] intValue] == 1) {
                //[self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
                [self goOutTime];
            }
            else
            {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        } @catch (NSException *exception) {
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        }
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES];
    
    /*
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       _emailField.text, @"email",nil];
    [self.view startLoadingAnimation];
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/send_forgot_mail" paramDictionary:requestDic onCompletion:^(NSString *responseString) {
        [self.view stopLoadingAnimation];
        @try {
            NSDictionary * dic = [responseString JSONValue];
            if ([[dic objectForKey:@"status"] intValue] == 1) {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
            else
            {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        } @catch (NSException *exception) {
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        }
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES];
    */
}

- (IBAction)nextAction:(id)sender {
    NSString * str_signl = self.signField.text;
    if (str_signl == nil || [str_signl isEqual:@""]) {
        [self showAlertView:@"温馨提示" subtitle:@"请输入验证码!"];
        return;
    }
    
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       _signField.text, @"code",nil];
    [self.view startLoadingAnimation];
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/verify_code_phone" paramDictionary:requestDic onCompletion:^(NSString *responseString) {
        [self.view stopLoadingAnimation];
        @try {
            
            NSDictionary* dic = [responseString JSONValue];
            if ([[dic objectForKey:@"status"] intValue] == 1) {
                SettingPwdViewController* vc = [[SettingPwdViewController alloc] initWithNibName:@"SettingPwdViewController" bundle:nil];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else
            {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        } @catch (NSException *exception) {
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        }
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES];
}

@end













